package cn.senwill.service.impl;

import cn.senwill.domain.Result;
import cn.senwill.domain.entity.Order;
import cn.senwill.feign.AccountFeignService;
import cn.senwill.feign.StockFeignService;
import cn.senwill.mapper.OrderMapper;
import cn.senwill.service.OrderService;
import com.alibaba.fastjson.JSONObject;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class OrderServiceImpl implements OrderService {

    private OrderMapper orderMapper;

    @Autowired
    public void setOrderMapper(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    private StockFeignService stockFeignService;

    @Autowired
    public void setStockFeignService(StockFeignService stockFeignService) {
        this.stockFeignService = stockFeignService;
    }

    private AccountFeignService accountFeignService;

    @Autowired
    public void setAccountFeignService(AccountFeignService accountFeignService) {
        this.accountFeignService = accountFeignService;
    }


    @Override
    @GlobalTransactional(name = "scorpios-add-order", rollbackFor = Exception.class)
    public Result add(JSONObject jsonObject) {
//        try {
            Long userId = Long.parseLong(String.valueOf(jsonObject.get("user_id")));
            Long productId = Long.parseLong(String.valueOf(jsonObject.get("product_id")));
            Integer count = Integer.parseInt(String.valueOf(jsonObject.get("count"))); // 下单数量
            BigDecimal money = new BigDecimal(String.valueOf(jsonObject.get("money"))); // 下单金额
            Order newOrder = new Order();
            newOrder.setUserId(userId);
            newOrder.setProductId(productId);
            newOrder.setCount(count);
            newOrder.setMoney(money);
            newOrder.setStatus(0);
            orderMapper.insert(newOrder);

            // 远程调用 account-service 服务的 update 接口
            JSONObject accountJsonObject = new JSONObject();
            accountJsonObject.put("user_id", userId);
            accountJsonObject.put("used", money);
            System.out.println("#### -> " + accountJsonObject.toJSONString());
            Result accountResult = accountFeignService.update(accountJsonObject);
            System.out.println(accountResult);

            // 远程调用 stock-service 服务的 update 接口
            JSONObject stockJsonObject = new JSONObject();
            stockJsonObject.put("product_id", productId);
            stockJsonObject.put("used", count);
            Result stockResult = stockFeignService.update(stockJsonObject);
            System.out.println(stockResult);

            // 修改订单状态为已支付
            newOrder.setStatus(1);
            orderMapper.updateById(newOrder);

            return Result.success("操作成功 ~", newOrder);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.error("操作失败 -> " + e.getMessage());
//        }
    }
}
